<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="nespresso.beans.*" %>
<%@page import="java.sql.*" %>
<%@page import="java.text.*" %>
<html>
<head>
<title>NESPRESSO</title>

<link rel="stylesheet" type="text/css" href="css/jquery-ui-1.8.17.custom.css" />
<link rel="stylesheet" type="text/css" href="css/ui.jqgrid.css" />
<link rel="stylesheet" type="text/css" href="css/nespresso.css" />

<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="js/grid.locale-fr.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.js" type="text/javascript"></script>

</head>

<body>
<div class="super-div">
<div align="right"><a href="nespresso.jsp">Déconnexion</a></div>
<div id="divrond1">
	<table width="100%">
		<tr height="83px"><td width="50%"></td><td align="center">	
			<p class="grosblancitalique"><b>Administration</b></p>
		</td></tr>
	</table>
</div>

<form action="administration" method="post" name="AdminForm" target="_self">
<div style="width:1020px; height:410px; text-align:center; padding:10px; margin:auto">
	<div id="tabs">
		<ul>
			<li><a href="#tabs-capsules">Capsules</a></li>
			<li><a href="#tabs-users">Utilisateurs</a></li>
			<li><a href="#tabs-params">Paramètres avancés</a></li>
		</ul>
		<div id="tabs-capsules" align="center">
		  	<table id="TabCapsule" style="font-size:11px;">
  				<tr>
  					<th>Id</th>
  					<th>Nom</th>
  					<th>Prix</th>
  					<th>Force</th>
  					<th>Image</th>
  				</tr>
  				<tbody>
  					<%	String pu = new String();
  						Base bc = new Base();
  						bc.GetResultat("CAPSULES");
  						while(bc.Suivant()) {
  							pu = new DecimalFormat("0.00").format(bc.GetReel("PRIX"));
  							%>
  							<tr>
  								<td><%=bc.GetTexte("ID") %></td>
  								<td><%=bc.GetTexte("NOM") %></td>
  								<td><%=pu %></td>
  								<td><%=bc.GetEntier("FORCE") %></td>
  								<td><%=bc.GetTexte("IMAGE") %></td>
  							</tr>
  							<%
  						}
  						bc.Free();
  					%>
			  	</tbody>
  			</table>
  			<div id="DivCapsule"></div>
		</div>
		<div id="tabs-users" align="center">
  			<table id="TabUser" style="font-size:11px;">
  				<tr>
  					<th>Id</th>
  					<th>Login</th>
  					<th>Nom</th>
  					<th>Prénom</th>
  					<th>Rôle</th>
  					<th>Email</th>
  				</tr>
  				<tbody>
  					<%	Base bu = new Base();
  						bu.GetResultat("USERS");
  						while(bu.Suivant()) { %>
  							<tr>
  								<td><%=bu.GetTexte("ID") %></td>
  								<td><%=bu.GetTexte("LOGIN") %></td>
  								<td><%=bu.GetTexte("NOM") %></td>
  								<td><%=bu.GetTexte("PRENOM") %></td>
  								<td><%=bu.GetTexte("ROLE") %></td>
  								<td><%=bu.GetTexte("EMAIL") %></td>
  							</tr>
  							<%
  						}
  						bu.Free();
  					%>
			  	</tbody>
  			</table>
  			<div id="DivUser"></div>
		</div>
		<div id="tabs-params" align="center">
			<table id="TabParam" style="font-size:11px;">
				<tr>
					<th>Id</th>
					<th>Paramètre</th>
					<th>Valeur</th>
					<th>Crypt</th>
				</tr>
				<tbody>
					<%	Base bp = new Base();
						bp.GetResultat("PARAMETRES");
  						while(bp.Suivant()) { %>
							<tr>
  								<td><%=bp.GetTexte("ID") %></td>
  								<td><%=bp.GetTexte("param_name") %></td>
  								<td><%=bp.GetTexte("param_value") %></td>
  								<td><%=((bp.GetEntier("param_crypt") == 1) ? "Oui" : "Non")  %></td>
  							</tr>
  							<%
  						}
  						bp.Free();
 		 			%>
  				</tbody>
  			</table>
  			<div id="DivParam"></div>
		</div>
	</div>
</div>
<div align="center">
	<input type="button" name="bcommande" id="bcNew" value="Retour" class="boutons" onclick="javascript:location.href='commandes.jsp'" /><br><br>
</div>
</form>
</div>

<script type="text/javascript" language="javascript">

tableToGrid("#TabParam", {	
	caption: 'Gestion des Paramètres',
    width: 'auto',
    height: 'auto',
    hidegrid: false,
    pager:'#DivParam',
    rowNum:10,
    forceFit:true,
    mtype:'POST',
    cellEdit: true,
    cellsubmit: 'remote',
    cellurl: 'Adminparam',
    comNames:['Id', 'Paramètre', 'Valeur', 'Cryptage ?'],
	colModel: [{name:'Id', editable:false, width:50},
               {name:'Paramètre', editable:true, editrules:{required:true}, width:200},
               {name:'Valeur', editable:true, width:200},
               {name:'Crypt', editable:true, edittype:'checkbox', editoptions: {value:"Oui:Non"}, width:80}],
	beforeSubmitCell: function(rowid, celname, value, iRow, iCol) {
		var rowData = jQuery(this).getRowData(rowid); 
    	var idParam= rowData['Id'];// On récupère l'Id du paramètre en cours d'édition
    	var cryptParam=rowData['Crypt'];// On récupère le cryptage pour savoir s'il faut crypter ou pas
		return {idParam:idParam,cryptParam:cryptParam}; }
});

tableToGrid("#TabUser", {
	caption: 'Gestion des Utilisateurs',
    width: 'auto',
    height: "auto",
    hidegrid: false,
    pager:'#DivUser',
    rowNum:10,
    cellEdit: true,
    cellsubmit: 'remote',
    cellurl: 'Adminuser',
	colModel: [{name:'Id', editable:false, width:50},
	           {name:'Login', editable:false, width:150},
               {name:'Nom', editable:true, width:200},
               {name:'Prénom', editable:true, width:200},
               {name:'Rôle', editable:true, width:80, edittype:'select',  editoptions: { multiple: false, value:{ADMIN:'ADMIN',GUEST:'GUEST'}}},
               {name:'Email', editable:true, width:200}],
    beforeSubmitCell: function(rowid, celname, value, iRow, iCol) {
    	var rowData = jQuery(this).getRowData(rowid); 
        var idUser= rowData['Id'];// On récupère l'Id du user en cours d'édition
    	return {idUser:idUser}; }
	
});

tableToGrid("#TabCapsule", {
	caption: 'Gestion des Capsules',
    width: 'auto',
    height: 'auto',
    hidegrid: false,
    pager:'#DivCapsule',
    rowNum:10,
    cellEdit: true,
    cellsubmit: 'remote',
    cellurl: 'Admincapsule',
	colModel: [{name:'Id', editable:false, width:50},
	           {name:'Nom', editable:true, width:200},
               {name:'Prix', editable:true, width:60, align:'right'},
               {name:'Force', editable:true, width:60, align:'right'},
               {name:'Image', editable:true, width:200, edittype:'file'}],
    beforeSubmitCell: function(rowid, celname, value, iRow, iCol) {
    	var rowData = jQuery(this).getRowData(rowid); 
        var idCapsule= rowData['Id'];// On récupère l'Id de la capsule en cours d'édition
    	return {idCapsule:idCapsule}; }	
});

$("#TabParam").trigger("reloadGrid");

$("#TabUser").navGrid('#DivUser',
		{edit:false,add:true,del:true,search:false},{},
		{width:500, url:'Adminuser',
			reloadAfterSubmit:true,
			onclickSubmit: function(param){ 
				var sr = jQuery('#TabUser').getGridParam('selrow');
				var idUser = jQuery('#TabUser').getCell(sr,'Id');
				return {idUser:idUser}; },
			afterSubmit: function(reponse, data) {
				console.log("Réponse : " + reponse);
				console.log("Data    : " + data);
				$("#eData").click();
				return [true,"Supression impossible !"];
			}
		});

$("#TabUser").trigger("reloadGrid");

$("#TabCapsule").navGrid('#DivCapsule',
		{edit:false,add:true,del:true,search:false},{},
		{width:500,url:'Admincapsule'},
		{width:500,url:'Admincapsule'});

$("#TabCapsule").trigger("reloadGrid");


</script>
</body>
</html>
